import { createRouter, createWebHistory } from 'vue-router'

import LoginPage from '@/views/LoginPage.vue'
import LayoutPage from '@/views/LayoutPage.vue'

import HomePage from '@/views/HomePage.vue';
import TeacherCenter from '@/views/TeacherCenter.vue';
import DataCenter from '@/views/DataCenter.vue';
import UserCenter from '@/views/UserCenter.vue';
import CourseCenter from '@/views/CourseCenter.vue';
import ActivityDetail from '@/views/ActivityDetail.vue'
import ActivityEdit from "@/views/ActivityEdit.vue";
import TeacherEdit from "@/views/TeacherEdit.vue";
import ClassCenter from "@/views/ClassCenter.vue";
import StudentCenter from "@/views/StudentCenter.vue";
import UnitCenter from "@/views/UnitCenter.vue";

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/LoginPage.vue')
    },
    // Root用户路由
    {
      path: '/root',
      component: () => import('@/views/LayoutPage.vue'),
      children: [
        {
          path: '',  // 默认子路由，作为root用户首页
          name: 'RootHome',
          component: () => import('@/views/UserCenter.vue'),
          meta: { requiresAuth: true, roles: ['root'] }
        },
        {
          path: 'base-data',
          name: 'BaseData',
          component: () => import('@/views/BaseDataManage.vue'),
          meta: { requiresAuth: true, roles: ['root'] }
        }
      ],
      meta: { requiresAuth: true, roles: ['root'] }
    },
    // Admin用户路由
    {
      path: '/admin',
      component: () => import('@/views/LayoutPage.vue'),
      children: [
        {
          path: '',  // 默认子路由，作为admin用户首页
          name: 'AdminHome',
          component: () => import('@/views/AdminDashboard.vue'),
          meta: { requiresAuth: true, roles: ['admin'] }
        },
        {
          path: 'course',
          name: 'AdminCourse',
          component: () => import('@/views/CourseCenter.vue'),
          meta: { requiresAuth: true, roles: ['admin'] }
        },
        {
          path: 'teacher',
          name: 'AdminTeacher',
          component: () => import('@/views/TeacherCenter.vue'),
          meta: { requiresAuth: true, roles: ['admin'] }
        },
        // 添加教师管理相关路由
        {
          path: 'teacher/add',
          name: 'TeacherAdd',
          component: () => import('@/views/TeacherEdit.vue'),
          meta: { requiresAuth: true, roles: ['admin'] }
        },
        {
          path: 'teacher/:teacherId/edit',
          name: 'TeacherEdit',
          component: () => import('@/views/TeacherEdit.vue'),
          meta: { requiresAuth: true, roles: ['admin'] }
        },
        {
          path: 'data',
          name: 'AdminData',
          component: () => import('@/views/DataCenter.vue'),
          meta: { requiresAuth: true, roles: ['admin'] }
        },
        // 活动详情路由
        {
          path: 'activity/:activityId',  // 修改这里，去掉开头的斜杠
          name: 'ActivityDetail',
          component: () => import('@/views/ActivityDetail.vue'),
          meta: { requiresAuth: true, roles: ['admin'] }
        },

        // 添加活动编辑路由
        {
          path: 'activity/:activityId/edit',
          name: 'ActivityEdit',
          component: () => import('@/views/ActivityEdit.vue'),
          meta: { requiresAuth: true, roles: ['admin'] }
        },

        // 添加新活动路由
        {
          path: 'activity/add',
          name: 'ActivityAdd',
          component: () => import('@/views/ActivityEdit.vue'),
          meta: { requiresAuth: true, roles: ['admin'] }
        },

        {
          path: 'student',
          name: 'AdminStudent',
          component: () => import('@/views/StudentCenter.vue'),
          meta: { requiresAuth: true, roles: ['admin'] }
        },
        {
          path: 'class',
          name: 'AdminClass',
          component: () => import('@/views/ClassCenter.vue'),
          meta: { requiresAuth: true, roles: ['admin'] }
        },
        // ... 其他admin路由
      ],
      meta: { requiresAuth: true, roles: ['admin'] }
    },
    // Teacher用户路由
    {
      path: '/teacher',
      component: () => import('@/views/LayoutPage.vue'),
      children: [
        {
          path: '',
          name: 'TeacherHome',
          component: () => import('@/views/Home.vue'),
          meta: { title: '工作首页', icon: 'home' }
        },
        {
          path: 'course',
          name: 'TeacherCourse',
          component: () => import('@/views/Course.vue'),
          meta: { title: '课程管理', icon: 'course' }
        },
        {
          path: 'attendance',
          name: 'TeacherAttendance',
          component: () => import('@/views/Attendance.vue'),
          meta: { title: '点名管理', icon: 'attendance' }
        },
        {
          path: 'class',
          name: 'TeacherClass',
          component: () => import('@/views/TeacherClass.vue'),
          meta: { title: '教学班管理', icon: 'class' }
        },
        {
          path: 'teaching',
          name: 'UnitCenter',
          component: () => import('@/views/UnitCenter.vue'),
          meta: { title: '教学单元管理', icon: 'unit' }
        }
      ]
    },
    // Student用户路由
    {
      path: '/student',
      component: () => import('@/views/LayoutPage.vue'),
      children: [
        {
          path: '',  // 默认子路由，作为student用户首页
          name: 'StudentHome',
          component: () => import('@/views/StudentDashboard.vue'),
          meta: { requiresAuth: true, roles: ['student'] }
        },
        {
          path: 'class',
          name: 'StudentClass',
          component: () => import('@/views/StudentClass.vue'),
          meta: { title: '我的班级', icon: 'StudentClass' }
        },
        {
          path: 'course',
          name: 'StudentCourse',
          component: () => import('@/views/StudentCourse.vue'),
          meta: { title: '我的课程', icon: 'StudentCourse' }
        },
        {
          path: 'call',
          name: 'StudentCalling',
          component: () => import('@/views/StudentCalling.vue'),
          meta: { title: '我要签到', icon: 'StudentCalling' }
        },
      ],
      meta: { requiresAuth: true, roles: ['student'] }
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  const userRole = localStorage.getItem('userRole')?.toLowerCase()

  if (to.path === '/login') {
    next()
    return
  }

  if (!token) {
    next('/login')
    return
  }

  // 检查路由前缀是否与用户角色匹配
  const rolePrefix = `/${userRole}`
  if (!to.path.startsWith(rolePrefix) && to.path !== '/login') {
    next('/login')
    return
  }

  // 检查路由是否需要特定角色
  if (to.meta.roles && !to.meta.roles.includes(userRole)) {
    next('/login')
    return
  }

  next()
})
export default router
